package com.nami.gate.router;

import com.nami.gate.router.config.CommandConfig;
import com.nami.gate.router.config.GateConfig;
import com.nami.gate.router.config.NacosConfig;
import com.nami.gate.router.config.rocketmq.ConsumerConfig;
import com.nami.gate.router.config.rocketmq.RocketMqConfig;
import com.nami.gate.router.utils.CommandParserUtil;
import com.nami.gate.router.utils.YamlReader;
import org.apache.commons.lang3.StringUtils;

import static com.nami.gate.router.constants.CommonConst.DEFAULT_CONFIG_FILE_PATH;

public class RouterStartup {

    public static void main(String[] args) {
        main0(createRouterController(args));
    }

    private static RouterController createRouterController(String[] args) {
        try {
            // 读取 配置
            YamlReader reader = new YamlReader(newCommandConfig(args));
            GateConfig gateConfig = file2Config(reader);
            return new RouterController(gateConfig);
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(-2);
        }
        return null;
    }
    private static CommandConfig newCommandConfig(String[] args) {
        CommandConfig config = new CommandConfig();
        config.setFilePath(DEFAULT_CONFIG_FILE_PATH);
        config.setIsDefaultPath(Boolean.TRUE);
        String argsConfigFilePath = CommandParserUtil.parseArgs(args);
        if (StringUtils.isNotBlank(argsConfigFilePath)) {
            config.setFilePath(argsConfigFilePath);
            config.setIsDefaultPath(Boolean.FALSE);
        }
        return config;
    }

    private static GateConfig file2Config(YamlReader reader) {
        GateConfig gateConfig = new GateConfig();
        gateConfig.setGatePort(reader.getIntegerValue("gate.port"));
        gateConfig.setIp(reader.getStrValue("gate.ip"));
        gateConfig.setHeartBeatInterval(reader.getIntegerValue("gate.heartBeatInterval"));
        gateConfig.setSslEnable(false);

        gateConfig.setMqType(reader.getStrValue("nami.mqType"));
        gateConfig.setRegisterType(reader.getStrValue("nami.registerType"));

        NacosConfig nacosConfig = new NacosConfig();
        nacosConfig.setServerAddr(reader.getStrValue("nacos.registerAddr"));
        nacosConfig.setNamespace(reader.getStrValue("nacos.namespace"));
        nacosConfig.setUsername(reader.getStrValue("nacos.username"));
        nacosConfig.setPassword(reader.getStrValue("nacos.password"));
        gateConfig.setNacosConfig(nacosConfig);

        RocketMqConfig mqConfig = new RocketMqConfig();
        mqConfig.setNamesrvAddr(reader.getStrValue("rocketmq.namesrvAddr"));
        mqConfig.setProducerGroup(reader.getStrValue("rocketmq.producerGroup"));
        mqConfig.setTags(gateConfig.getIp() + ":" + gateConfig.getGatePort());

        ConsumerConfig consumerConfig = new ConsumerConfig();
        consumerConfig.setLoginConsumerGroup(reader.getStrValue("rocketmq.consumer.loginConsumerGroup"));
        consumerConfig.setPublishConsumerGroup(reader.getStrValue("rocketmq.consumer.publishConsumerGroup"));
        mqConfig.setConsumerConfig(consumerConfig);
        gateConfig.setRocketMqConfig(mqConfig);


        return gateConfig;
    }


    private static void main0(RouterController controller) {
        // 2. 启动
        try {
            controller.start();
        } catch (Exception ex) {
            ex.printStackTrace();
            System.exit(1);
        }
    }

}
